﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Sharkbite.Irc;

namespace CSharpIrcBot
{
    public partial class MainForm : Form
    {
        private SortedList<string, Templates.ChannelTemplate> slChannelNameToChannelForm;
        Connection CurrentConnection;

        public MainForm()
        {
            InitializeComponent();
        }
        
        private void mnuFileConnection_Click( object sender, EventArgs e )
        {
            Forms.frmOptions Options = new CSharpIrcBot.Forms.frmOptions();
            Options.ShowDialog();
            //SaveSettings();
            LoadSettings();
        }

        private void SaveSettings()
        {
            throw new NotImplementedException();
        }

        private void MainForm_FormClosing( object sender, FormClosingEventArgs e )
        {
            if( Global.ircConnection != null && Global.ircConnection.Connected )
            {
                Global.Plugins.ClosePlugins();
                Global.ircConnection.Disconnect(String.Format("{0} decided to go recharge in the real world.", Global.ircConnection.ConnectionData.Nick));
            }
            if(!string.IsNullOrEmpty(Global.ConnectionInfo.Hostname))
            {
                CSharpIrcBot.Properties.Settings.Default.LastHostName = Global.ConnectionInfo.Hostname;
            }
            CSharpIrcBot.Properties.Settings.Default.Save();
        }

        private void mnuFileExit_Click( object sender, EventArgs e )
        {
            this.Close();
        }

        private void MainForm_Load( object sender, EventArgs e )
        {
            LoadSettings();
        }

        private void LoadSettings()
        {
            string hostName = CSharpIrcBot.Properties.Settings.Default.LastHostName;
            if( !string.IsNullOrEmpty( hostName ) )
            {
                CurrentConnection = ConnectionSettings.LoadConnections()[hostName];
                if( CurrentConnection != null )
                {
                    Global.ConnectionInfo = CurrentConnection.GetConnectionArgs();
                }
            }
        }

        private void tsbConnect_Click( object sender, EventArgs e )
        {
            if(Global.ircConnection == null || !Global.ircConnection.Connected )
            {
                if( string.IsNullOrEmpty( Global.ConnectionInfo.Hostname ) )
                {
                    if( CurrentConnection == null )
                    {
                        MessageBox.Show( "Please set up a server in Option > Connections", "No Server Set Up" );
                        return;
                    }
                    Global.ConnectionInfo = CurrentConnection.GetConnectionArgs();
                }
                else
                {
                    Global.ircConnection = new Sharkbite.Irc.Connection( Global.ConnectionInfo, true, false );
                    SetupChannels();
                    Identd.Start( Global.ConnectionInfo.Nick );
                    CtcpResponder autoResponder = new CtcpResponder( Global.ircConnection );
                    autoResponder.SourceResponse = "http://www.codeplex.com/CSharpIrcBot";
                    autoResponder.UserInfoResponse = "CSharpIrcBot CTCP Responder";
                    autoResponder.VersionResponse = "CSharpIrcBot 1.0  - Powered by Thresher IRC Library 1.1";
                    autoResponder.ResponseDelay = 2000;

                    Global.ircConnection.Listener.OnRegistered += new RegisteredEventHandler( Listener_OnRegistered );

                    Global.ircConnection.Connect();
                }
            }
        }

        private void SetupChannels()
        {
            if( slChannelNameToChannelForm == null )
            {
                slChannelNameToChannelForm = new SortedList<string, CSharpIrcBot.Templates.ChannelTemplate>();
            }
            foreach( string channel in CurrentConnection.Channels )
            {
                slChannelNameToChannelForm.Add( channel, new CSharpIrcBot.Templates.ChannelTemplate( channel ) );
                slChannelNameToChannelForm[channel].MdiParent = this;
                slChannelNameToChannelForm[channel].Show();
                slChannelNameToChannelForm[channel].Dock = DockStyle.Fill;
            }
        }

        void Listener_OnRegistered()
        {
            Identd.Stop();
            JoinChannels();
        }

        private void JoinChannels()
        {
            foreach( string channel in CurrentConnection.Channels )
            {
                Global.ircConnection.Sender.Join( channel );
            }
            Global.ircConnection.Listener.OnRegistered -=new RegisteredEventHandler(Listener_OnRegistered);
        }

    }
}
